package com.example.demo01.Impl.webImpl;

import com.example.demo01.Dao.webDao.HospitalDao;
import com.example.demo01.Entity.webEntity.Hospital;
import com.example.demo01.utils.JDBCPGUtils;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class HospitalDaoImpl implements HospitalDao {

    private Connection connection = null;
    private PreparedStatement statement = null;
    private ResultSet resultSet = null;

    @Override
    public List<Hospital> getAllHospitals() {
        String sqlQuery = "select name,lat,lng,address,province,city,domain,uid,geom from hospital";
        List<Hospital> list = new ArrayList<>();
        Hospital hospital = null;
        try {
            //1-获取数据库连接
            connection = JDBCPGUtils.getConnection();
            //2-获取Statement实例
            statement = connection.prepareStatement(sqlQuery);
            //3-执行SQL语句，获取ResultSet实例
            resultSet = statement.executeQuery();
            //4-解析查询结果
            while (resultSet.next()) {
                hospital = new Hospital(
                        resultSet.getString(1),
                        resultSet.getFloat(2),
                        resultSet.getFloat(3),
                        resultSet.getString(4),
                        resultSet.getString(5),
                        resultSet.getString(6),
                        resultSet.getString(7),
                        resultSet.getString(8),
                        resultSet.getString(9)
                );
                list.add(hospital);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCPGUtils.close(resultSet, statement, connection);
        }
        return list;
    }
}
